﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using DataContracts;
using System.Web.UI.WebControls;

namespace TrainingWorkshop.MVPWebsite.Authority.Views
{
    public partial class AuthorityList : Microsoft.Practices.CompositeWeb.Web.UI.Page, IAuthorityListView
    {
        private AuthorityListPresenter _presenter;

        #region Property

        public int AuthorityId { get; set; }
        public string AuthorityName
        {
            get
            {
                return this.AuthorityNameTextBox.Text;
            }
        }
        public int LevelId
        {
            get
            {
                return Convert.ToInt32(this.LevelDropDownList.SelectedValue);
            }
        }
        public string LevelName
        {
            get
            {
                return this.LevelDropDownList.SelectedItem.Text;
            }
        }

        public IList<AuthorityLevelDTO> AuthorityLevelList
        {
            set
            {
                this.LevelDropDownList.DataSource = value;
                this.LevelDropDownList.DataTextField = "LevelName";
                this.LevelDropDownList.DataValueField = "LevelId";
                this.LevelDropDownList.DataBind();
                LevelDropDownList.Items.Insert(0, new ListItem("Please Selected", "0"));
            }
        }

        public IList<AuthorityDTO> SearchAuthorityList
        {
            set
            {
                authorityListObjectContainerDataSource.DataSource = value;
                authorityListObjectContainerDataSource.DataBind();
            }
        }

        public IList<AuthorityDTO> AuthorityDetailsList
        {
            set
            {
                authorityListObjectContainerDataSource.DataSource = value;
                authorityListObjectContainerDataSource.DataBind();
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                _presenter.OnViewInitialized();
            }
            _presenter.OnViewLoaded();
        }

        [CreateNew]
        public AuthorityListPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            _presenter.SearchAuthority();
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int id = int.Parse(btn.CommandArgument);
            this.AuthorityId = id;
            this._presenter.DeleteAuthority();
            _presenter.OnViewInitialized();
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int id = int.Parse(btn.CommandArgument);
            _presenter.UpdateCheck(id);
            _presenter.OnViewInitialized();
        }

        public void ShowOperationMessage(string message)
        {
            //Response.Write("<script language=javascript>alert('" + message + "')</script>");
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }

        public void Redirect(string url)
        {
            Response.Redirect(url);
        }
	}
}

